package com.squid.cloud.common.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.squid.cloud.common.enums.ErrorCodeEnum;
import com.squid.cloud.common.exception.SquidException;

import lombok.extern.slf4j.Slf4j;

/** 
 * @author 黄飞
 * @project pay-gather-model
 * @title SmsUtils
 * @Description: TODO
 * @date 2016年7月28日 下午5:21:20
 * @Company 重庆爱车联信息科技技术有限公司
 * @Copyright 2016
 * @version V1.0
 */
@Slf4j
public class SmsUtils {


	// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
	public static final String Ali_APP_KEY="LTAI2cdWK1aZHuoY";
	public static final String Ali_APP_SECRET="dDnPbcCe2tfCm5qeXygbqGNirHjqeG";
	public static final String SIGN="鱿鱼加油";

	//产品名称:云通信短信API产品,开发者无需替换
	public static final String product = "Dysmsapi";
	//产品域名,开发者无需替换
	public static final String domain = "dysmsapi.aliyuncs.com";
	// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
	static final String accessKeyId = "LTAI2cdWK1aZHuoY";
	static final String accessKeySecret = "dDnPbcCe2tfCm5qeXygbqGNirHjqeG";




	public static void sendSms(Map<String,String> param) throws ClientException {

		//可自助调整超时时间
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		//初始化acsClient,暂不支持region化
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		//组装请求对象-具体描述见控制台-文档部分内容
		SendSmsRequest request = new SendSmsRequest();
		//必填:待发送手机号
		request.setPhoneNumbers(param.get("number"));
		//必填:短信签名-可在短信控制台中找到
		request.setSignName(param.get("sign"));
		//必填:短信模板-可在短信控制台中找到
		request.setTemplateCode(param.get("template"));
		//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
		request.setTemplateParam(param.get("param"));

		//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
		//request.setSmsUpExtendCode("90997");

		//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
		//request.setOutId("yourOutId");

		//hint 此处可能会抛出异常，注意catch
		try{
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			log.info("======>阿里大于短信发送结果："+JsonUtils.objectToJson(sendSmsResponse) );
			if(!"OK".equals(sendSmsResponse.getCode())){
				if(!"isv.BUSINESS_LIMIT_CONTROL".equals(sendSmsResponse.getCode())){
					throw new ClientException("短信发送失败");
				}else{
					log.info("触发阿里分流："+sendSmsResponse.getMessage());
				}
			
			}
		}catch (ClientException e) {
			log.error("短信发送失败",e );
			throw e;
		}
	}


	public static AliSmsUtil sendAliSms(){
		return new AliSmsUtil(Ali_APP_KEY, Ali_APP_SECRET);
	}

	public static AliParams buliderAliParams(){
		return new AliParams();
	}

	public static Params buliderAliBussParams(){
		return new Params();
	}

	public static enum AliTemplateEnum{

		//验证类短信
		SMS_VERIFICATION("SMS_113450845","尊敬的客户您好，您的验证码为${code}，请及时使用。"){

			@Override
			public void sendVerification(String code, String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Params params = SmsUtils.buliderAliBussParams();
				params.setCode(code);
				Map<String, String> busMap=params.smsVerification();
				aliMap.put("param",AliSmsUtil.mapToString((LinkedHashMap<String, String>) busMap));
				sendSms(aliMap);
			}
		},
        //SMS_113660199
		SMS_APP_SYSTEM_UPDATE("SMS_114060437","尊敬的客户您好，系统已经升级，请及时更新APP。猛戳www.youyuer.com/app/start.html打开APP-回复T退订"){
			@Override
			public void sendMsg(String phone) throws Exception{
                log.info("系统升级消息发送短信电话号码：" + phone);
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				sendSms(aliMap);
			}
		},
		SMS_DISCOUNT_ACCOUNT_GIVE("SMS_116582617","尊敬的客户您好，我们已为您的折扣账户存入可用于折扣的加油金额${money}，请登录APP查看详情。"){
			@Override
			public void sendMsg(String phone,String param) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("money",param);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_REGIST_NOTICE("SMS_116561645","尊敬的客户您好，恭喜您已经成功注册为鱿鱼加油会员。"){
			@Override
			public void sendMsg(String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				sendSms(aliMap);
			}
		},
//		SMS_DEDUCT_MONEY_NOTICE("SMS_116567322","尊敬的客户您好，您的购油账户本次消费油量价值${money}元，剩余油量价值${balance}元，请登录APP查看扣款详情。"){
		//                                       尊敬的客户您好，您的购油账户本次消费油量价值${money}元，剩余油量价值${balance}元。
		SMS_DEDUCT_MONEY_NOTICE("SMS_127158488","尊敬的客户您好，您的购油账户本次消费油量价值${money}元，剩余油量价值${balance}元。"){
			@Override
			public void sendMsg(String phone,String money,String balance, String oil) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("money",money);
				map.put("balance",balance);
//				map.put("oil",oil);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_FORGET_NOTICE("SMS_127153552","尊敬的客户您好，您的账户密码已经于${date}修改成功"){
			@Override
			public void sendMsg(String phone,String param) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("date",param);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_PAY_PWD_MODIFY_NOTICE("SMS_113660179","尊敬的客户您好，您的交易密码已经修改成功，请注意保密。"){
			@Override
			public void sendMsg(String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				sendSms(aliMap);
			}
		},
		SMS_BALANCE_LESS_NOTICE("SMS_116592371","尊敬的客户您好，您的${oil}购油账户油品余量已经不多，请及时购油。"){
			@Override
			public void sendMsg(String phone, String oil) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("oil",oil);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_DISCOUNT_LESS_NOTICE("SMS_116582446","尊敬的客户您好，您的${discount}折扣账户额度已经不多，请及时参加鱿鱼活动获取更多购油优惠。"){
			@Override
			public void sendMsg(String phone, String discount) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("discount",discount);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_INIT_PWD("SMS_113660182","尊敬的客户您好，您的登录密码已于${date}重置了密码，密码为${password}，为了您的账户安全请尽快修改密码。"){
			@Override
			public void sendMsg(String phone,String date,String password) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("date",date);
				map.put("password",password);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_PAPER_INVOICE_SUCCESS("SMS_119085017","尊敬的客户您好，您申请票据编号为${invoiceNo}已成功，快递公司${express}，快递编号${logisticsNo}，请注意查收。"){
			@Override
			public void sendMsg(String phone,String invoiceNo,String express,String logisticsNo) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("invoiceNo",invoiceNo);
				map.put("express",express);
				map.put("logisticsNo",logisticsNo);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_ELECTRONIC_INVOICE_SUCCESS("SMS_113451188","尊敬的客户您好，您申请的发票编号${invoiceNo}已开票成功，请注意查收。"){
			@Override
			public void sendMsg(String phone,String param) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("invoiceNo",param);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_PAPER_INVOICE_FAIL("SMS_113455984","尊敬的客户您好，您申请的发票编号${invoiceNo}未通过审核，邮费已退款。"){
			@Override
			public void sendMsg(String phone,String param) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("invoiceNo",param);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_ELECTRONIC_INVOICE_FAIL("SMS_113660481","尊敬的客户您好，您发票申请编号${invoiceNo}未通过审核。"){
			@Override
			public void sendMsg(String phone,String param) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("invoiceNo",param);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		SMS_REFUEL_REFUND("SMS_127159660","尊敬的客户您好，您的加油订单${orderNo}已退款成功，本次退款油量价值${money}元，购油账户剩余油量价值${balance}元。"){
			@Override
			public void sendMsg(String phone,String orderNo, String money, String balance) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("orderNo",orderNo);
				map.put("money",money);
				map.put("balance",balance);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//验证类短信
		SMS_LOST_CARD_VERIFICATION("SMS_122297861","尊敬的客户您好，您的中石化油卡账户正在申请挂失,验证码为${code}，请及时使用。"){
			@Override
			public void sendLostCardVerification(String code, String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Params params = SmsUtils.buliderAliBussParams();
				params.setCode(code);
				Map<String, String> busMap=params.smsVerification();
				aliMap.put("param",AliSmsUtil.mapToString((LinkedHashMap<String, String>) busMap));
				sendSms(aliMap);
			}
		},

		//挂失失败短息
		SMS_LOST_OIL_CARD_FAILURE("SMS_122292809","尊敬的客户您好，您的中石化油卡号${cardNo}账户挂失申请被驳回。请咨询电话023 88307160。"){
			@Override
			public void sendMsg(String phone, String cardNo) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("cardNo",cardNo);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//挂失成功短息
		SMS_LOST_OIL_CARD_SUCCESS("SMS_122292677","尊敬的客户您好，您的中石化油卡号${cardNo}账户挂失已经成功处理，该卡片将不能享受鱿鱼加油相关服务。人工服务请咨询电话023 88307160 。"){
			@Override
			public void sendMsg(String phone, String cardNo) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("cardNo",cardNo);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//挂失申请短信
		SMS_LOST_OIL_CARD("SMS_122282729","尊敬的客户您好，您的中石化油卡号${cardNo}账户挂失申请已经受理，我们将在48小时候内生效。如有异议请咨询电话023 88307160。"){
			@Override
			public void sendMsg(String phone, String cardNo) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("cardNo",cardNo);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//换卡成功受理短信
		SMS_CHANGE_OIL_CARD("SMS_122292711","尊敬的客户您好，您的换卡申请已经受理，我们将在48小时候内生效。如有异议请咨询电话023 88307160。"){
			@Override
			public void sendMsg(String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},


		//换卡成功短信
		SMS_CHANGE_OIL_CARD_SUCCESS("SMS_122282702","尊敬的客户您好，您的中石化油换卡已经成功处理。卡号为${cardNo}，原来卡片已经作废。人工服务请咨询电话023 88307160 。"){
			@Override
			public void sendMsg(String cardNo, String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("cardNo",cardNo);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//嗨卡注册通知
		SMS_HICARD_REGIST_NOTICE("SMS_127168857"," 尊敬的${phoneNo}用户您好，${name}通过鱿鱼加油已经成功为您创建加油账户，您的密码为：${password}，客服电话 400 0606 569。"){
			@Override
			public void sendMsg(String phone, String passWord, String name) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("phoneNo",phone);
				map.put("name",name);
				map.put("password",passWord);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//嗨卡上账通知
		SMS_HICARD_ACCOUNT_NOTICE("SMS_127168856","尊敬的${phoneNo}用户您好，${name}已经成功为您的鱿鱼加油账户上账：${yuan}。"){
			@Override
			public void sendMsg(String phone, String amount, String name) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("phoneNo",phone);
				map.put("name",name);
				map.put("yuan",amount);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//换卡失败短信
		SMS_CHANGE_OIL_CARD_FAILURE("SMS_122282793","尊敬的客户您好，您换卡申请被驳回。请咨询电话023 88307160。"){
			@Override
			public void sendMsg(String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},

		//换卡失败短信
		SMS_EXCHANGE_CARD_VERIFICATION("SMS_122289126","尊敬的客户您好，您的中石化油卡账户正在申请换卡,验证码为${code}，请及时使用。"){
			@Override
			public void sendLostCardVerification(String code, String phone) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Params params = SmsUtils.buliderAliBussParams();
				params.setCode(code);
				Map<String, String> busMap=params.smsVerification();
				aliMap.put("param",AliSmsUtil.mapToString((LinkedHashMap<String, String>) busMap));
				sendSms(aliMap);
			}
		},
		// 备付金余额小于通知限额短信通知
		SMS_BALANCE_SURPASS_LIMIT("SMS_134324565","商户:${name} 备付金账户余额为:${amount}元，已经小于限额，请及时充值。"){
			@Override
			public void sendMsg(String phone, String amount, String name) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("name",name);
				map.put("amount",amount);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		// 油站主/副卡余额小于通知限额短信通知
		SMS_OIL_MATSTER_AND_VICE_BALANCE_SURPASS_LIMIT("SMS_135033625","商户:${name} 备付金账户余额为:${amount}元，已经小于限额，请及时充值。"){
			@Override
			public void sendMsg(String phone, String amount, String cardNo,String head) throws Exception{
				Map<String, String> aliMap=SmsUtils.buliderAliParams().setNumber(phone).setSign(SIGN).setTemplate(this.getCode()).bulierMap();
				Map<String,String> map=new HashMap<String, String>();
				map.put("name",cardNo);
				map.put("head",head);
				map.put("amount",amount);
				aliMap.put("param", JsonUtils.objectToJson(map));
				sendSms(aliMap);
			}
		},
		;

		private AliTemplateEnum(String code, String message) {
			this.code = code;
			this.message = message;
		}

		public void sendVerification(String code, String phone) throws Exception{

		}

		public void sendMsg(String phone) throws Exception{

		}

		public void sendMsg(String phone,String params) throws Exception{

		}
		public void sendMsg(String phone,String param1,String param2) throws Exception{

		}

		public void sendMsg(String phone,String param1,String param2,String param3) throws Exception{

		}

		public void sendMsg(String phone,String param1,String param2,String param3, String param4) throws Exception{

		}

		/**
		 * 油卡挂失发送通知短信
		 * @param code 验证码
		 * @param phone 电话号码
		 * @throws Exception 异常
		 */
		public void sendLostCardVerification(String code, String phone) throws Exception{

		}

		private final String code;

		private final String message;

		public String getCode() {
			return code;
		}

		public String getMessage() {
			return message;
		}
	}


	//阿里短信请请求参数
	public static class AliParams{

		private String sign;

		private String number;

		private String template;

		public String getSign() {
			return sign;
		}

		public AliParams setSign(String sign) {
			this.sign = sign;
			return this;
		}

		public String getNumber() {
			return number;
		}

		public AliParams setNumber(String number) {
			this.number = number;
			return this;
		}

		public String getTemplate() {
			return template;
		}

		public AliParams setTemplate(String template) {
			this.template = template;
			return this;
		}

		public Map<String, String> bulierMap(){
			return MapUtils.beanToStringMapExcludeEmpty(this);
		}
	}

	public static class Params{

		private String code;

		private String product;

		private String station;

		private String time;

		private String money;

		private String status;

		/**
		 * @throws SquidException 
		 * 发送注册验证短信
		 * @Title: smsRegister
		 * @Description: TODO
		 * @author: 黄飞
		 * @return LinkedHashMap<String,String>
		 * @return
		 * @throws
		 */
		public LinkedHashMap<String, String> smsVerification() throws SquidException{
			LinkedHashMap<String, String> temp = new LinkedHashMap<String, String>(); 
			if(StringUtils.isEmpty(code)){
				throw new SquidException(ErrorCodeEnum.SMSCODEISEMPTY);
			}
			temp.put("code", code);
			return temp;
		}

		public String getCode() {
			return code;
		}

		public Params setCode(String code) {
			this.code = code;
			return this;
		}

		public String getProduct() {
			return product;
		}

		public Params setProduct(String product) {
			this.product = product;
			return this;
		}

		public String getStation() {
			return station;
		}

		public Params setStation(String station) {
			this.station = station;
			return this;
		}

		public String getTime() {
			return time;
		}

		public Params setTime(String time) {
			this.time = time;
			return this;
		}

		public String getMoney() {
			return money;
		}

		public Params setMoney(String money) {
			this.money = money;
			return this;
		}
		public String getStatus() {
			return status;
		}

		public Params setStatus(String status) {
			this.status = status;
			return this;
		}

	}

}


